package com.hzw.code;


import com.hzw.code.util.IDCardUtil;
import com.hzw.code.util.RandomValue;
import com.hzw.code.util.WeightRandomMz;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;

public class Test {

    public static void main(String[] args) {
        int sex = 0;
        String card = null;
        int c = 1;
        for(int k=1;k<=3000; k++) {
            StringBuffer sbText = new StringBuffer();
            for (int i = 1; i <= 100000; i++) {
                card = IDCardUtil.generateID();
                if (Integer.parseInt(card.substring(16, 17)) % 2 == 1) {
                    sex = 1;
                } else {
                    sex = 0;
                }
                sbText.append(c++).append(",").
                        append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")).
                        append(",").append(RandomValue.getTel()).
                        append(",").append(card).
                        append(",").append(sex).
                        append(",").append(WeightRandomMz.getMz()).
                        append(",").append(RandomValue.getChineseName()).
                        append(",").append(RandomValue.getRoad()).append("\n");
                System.out.println("当前条数："+c);
            }
            write("D:\\app\\ld\\users_data.txt", sbText.toString());
        }
    }


    /**
     * 追加写入文件
     * @param file  文件路径
     * @param text  追加写入内容
     */
    public static void write(String file,String text) {
        FileOutputStream fos = null;
        FileChannel channel = null;
        try {
            fos = new FileOutputStream(file,true);
            channel = fos.getChannel();
            byte[] array = text.getBytes();
            ByteBuffer buffer = ByteBuffer.wrap(array);
            channel.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
